<template>
  <div class="stock-record-export">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="产品">
        <el-select v-model="exportParams.product_id" placeholder="请选择产品">
          <el-option v-for="item in products" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="操作类型">
        <el-select v-model="exportParams.type" placeholder="请选择类型">
          <el-option label="入库" value="1"></el-option>
          <el-option label="出库" value="2"></el-option>
          <el-option label="盘点" value="3"></el-option>
          <el-option label="调拨" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间">
        <el-date-picker v-model="exportParams.beginTime" type="datetime" placeholder="选择开始时间"></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间">
        <el-date-picker v-model="exportParams.endTime" type="datetime" placeholder="选择结束时间"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="exportRecords">导出</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'

const products = ref([])
const exportParams = ref({
  product_id: '',
  type: '',
  beginTime: '',
  endTime: ''
})

// 获取产品列表
const getProducts = async () => {
  const res = await request.get('/api/product/')
  products.value = res.records
}

// 导出库存记录
const exportRecords = () => {
  window.open(`/api/stock-record/export/?product_id=${exportParams.value.product_id}&type=${exportParams.value.type}&beginTime=${exportParams.value.beginTime}&endTime=${exportParams.value.endTime}`)
}

onMounted(() => {
  getProducts()
})
</script>

<style scoped>
.stock-record-export {
  padding: 20px;
}
</style>
